package omm.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import omm.dao.DatabaseStrategy;
import omm.dao.QuotationDAO;
import omm.domain.ConnectionManager;
import omm.domain.Quotation;
import omm.domain.Quotation.State;
import omm.domain.evolutiongraph.ClientsQuantityUnitStrategy;
import omm.domain.exceptions.DatabaseException;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DatabaseQuotationDAO implements QuotationDAO {

	private static final Logger logger = Logger.getLogger(DatabaseQuotationDAO.class);
	private ConnectionManager manager;

	@Autowired
	public DatabaseQuotationDAO(ConnectionManager manager, DatabaseOrderDAO databaseOrderDAO) {
		this.manager = manager;
	}

	@Override
	public List<Quotation> getAllWithFilter(DatabaseStrategy ds, DateTime beginning, DateTime ending) {
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		List<Quotation> quotations = new ArrayList<Quotation>();
		int won = 0, lost = 0, quoted = 0;
		int mwon = 0, mlost = 0, mquoted = 0;
		try {
			stmt = ds.getStatement(conn, beginning, ending);
			ResultSet cur = stmt.executeQuery();

			cur = stmt.executeQuery();
			while (cur.next()) {
				Quotation quotation = ds.makeQuotation(cur);

				// TODO: Ver si se aplica algun filtro
				if ((quotation.getState() == State.LOST) || (quotation.getState() == State.WON) 
						|| ((quotation.getState() == State.QUOTED) && quotation.getAonCommision() != 0.00)) {
					quotations.add(quotation);
					State state = quotation.getState();
					if (state == State.WON) {
						won++;
						mwon += quotation.getAonCommision();
						// System.out.println("GANADA: "+quotation.toString());
					}
					if (state == State.LOST) {
						lost++;
						mlost += quotation.getAonCommision();
					}
					if (state == State.QUOTED) {
						quoted++;
						mquoted += quotation.getAonCommision();
					}
				}
				// // Aplico filtro
				// State state = quotation.getState();
				// if (state == State.NULL) {
				// quotations.add(quotation);
				//
				// } else if (state == State.WON || state == State.LOST) {
				// quotations.add(quotation);
				// cur.next();
				// cur.next();
				//
				// } else if (state == State.QUOTED &&
				// quotation.getAonCommision() != 0.00) {
				// quotations.add(quotation);
				// cur.next();
				// cur.next();
				//
				// } else if (state == State.NO_INTEREST || state ==
				// State.PARTICIPATED
				// || (state == State.QUOTED && quotation.getAonCommision() ==
				// 0.00)) {
				// cur.next();
				//
				// quotation = ds.makeQuotation(cur);
				// state = quotation.getState();
				// if (state == State.NO_INTEREST || state == State.PARTICIPATED
				// || (state == State.QUOTED && quotation.getAonCommision() ==
				// 0.00)) {
				// cur.next();
				//
				// quotation = ds.makeQuotation(cur);
				// quotations.add(quotation);
				//
				// } else {
				// quotations.add(quotation);
				// cur.next();
				// }
				// }

			}
		} catch (Exception e) {
			throw new DatabaseException();
		}

		//Iterator<Quotation> it = quotations.iterator();
		// TODO:Ver si queda:
		// Filtro Perl: stSTATUS3_QUANTITIES_BY_YEAR
		// while(it.hasNext()){
		// Quotation q = it.next();
		// if(ending.isAfter(q.getModificationDate()) &&
		// beginning.isBefore(q.getModificationDate()) ){
		// quotations1.add(q);
		//
		// }
		// }
		manager.closeConnection(conn);
		
		logger.debug("TODAS LAS COTIZACIONES FILTRADAS:");
		for (Quotation q : quotations) {
			 logger.debug(q.toString());
		 }
		System.out.println("==========================IGUAL QUE EN EMPRESAS=======================================================");
		System.out.println("==============================COTIZACIONES:=================================================");
		System.out.println("===========================GANADA: " + won + "PERDIDA: " + lost + "COTIZADA: " + quoted
				+ "========================================");
		System.out.println("===========================GANADA: " + mwon + "PERDIDA: " + mlost + "COTIZADA: " + mquoted
				+ "=====================================");
		System.out.println("=================================================================================================");
		// return quotations1;
		return quotations;
	}

}
